//
//  UITools.swift
//  sss
//
//  Created by HOrange on 2020/2/21.
//  Copyright © 2020 hrrMac. All rights reserved.
//

import UIKit

///前缀类型
struct YO<Base> {
    var base:Base
    init(_ base: Base) {
        self.base = base
    }
    
    /// .base 或者end 都可以得到控件对象(UILabel,UIView 等)，只不过end更加直观
    @discardableResult
    func end() -> Base {
        return base
    }
}

///利用协议扩展前缀属性
protocol YOCompatible {}

extension YOCompatible{
    var yo: YO<Self>{
        set {}
        get { YO(self) }
    }
    static var yo: YO<Self>.Type {
        set {}
        get { YO<Self>.self }
        
    }
}

// 拥有yo 前缀属性
extension UIView: YOCompatible{}
//给UIView 扩展前缀功能
extension YO where Base == UIView{
    
    @discardableResult
    func backgroundColor(_ color: String) -> YO{
        base.backgroundColor = UIColor.hex(color)
        return base.yo
    }
    
    @discardableResult
    func cornerRadiusAndClip(_ radius: CGFloat) -> YO{
        base.layer.cornerRadius = radius
        base.clipsToBounds = true
        return base.yo
    }
}

//extension UILabel: YOCompatible{}
extension YO where Base == UILabel{
    
    @discardableResult
    func text(_ str: String) -> YO{
        base.text = str
        return base.yo
    }
    
    @discardableResult
    func font(_ font: CGFloat) -> YO{
        base.font = UIFont.systemFont(ofSize: font)
        return base.yo
    }
    
    @discardableResult
    func textColor(_ color: String) -> YO{
        base.textColor = UIColor.hex(color)
        return base.yo
    }
    
    @discardableResult
    func lBackgroundColor(_ color: String) -> YO{
        base.backgroundColor = UIColor.hex(color)
        return base.yo
    }
    
    
    @discardableResult
    func textAlignment(_ ali: NSTextAlignment) -> YO{
        base.textAlignment = ali
        return base.yo
    }
    
}


//
extension YO where Base == UIButton{
    
    @discardableResult
    func text(_ text: String) -> YO{
        base.setTitle(text, for: UIControl.State.normal)
        return base.yo
    }
    
    @discardableResult
    func textColor(_ color: String) -> YO{
        base.setTitleColor(UIColor.hex(color), for: UIControl.State.normal)
        return base.yo
    }
    
    @discardableResult
    func img(_ img: String) -> YO{
        base.setImage(UIImage(named: img), for: UIControl.State.normal)
        return base.yo
    }
    
    @discardableResult
    func backgroundColor(_ color: String) -> YO{
        base.backgroundColor = UIColor.hex(color)
        return base.yo
    }
    
}


extension UIButton{
    //----------------按钮点击回调事件
    /*
     按钮回调事件
     使用方法
     btn.btnTouch {
         
     }
     */
    private static var btnEventBlockKey: Void? //需要一个地址，存放对象的指针
    
    private var btnEvnetClickBlock:(()->())? {
        get{
            return objc_getAssociatedObject(self, &Self.btnEventBlockKey) as? ()->()
        }
        set{
            objc_setAssociatedObject(self, &Self.btnEventBlockKey, newValue, .OBJC_ASSOCIATION_COPY)
        }
    }
    
    func btnTouch (_ controlEvents : UIControl.Event = .touchUpInside,_ action:@escaping (()->())){
        btnEvnetClickBlock = action
        addTarget(self, action: #selector(btnClick), for: controlEvents)
    }
    
    
    @objc private func btnClick(){
        if btnEvnetClickBlock != nil {
            self.btnEvnetClickBlock!()
        }
    }
}




extension UIColor{
    convenience init(r:UInt32 ,g:UInt32 , b:UInt32, a:CGFloat = 1.0) {
        self.init(red: CGFloat(r) / 255.0,
                  green: CGFloat(g) / 255.0,
                  blue: CGFloat(b) / 255.0,
                  alpha: a)
    }
    
    class func hex(_ hexString : String) ->UIColor{
        //取出掉空格或者换行符号
        var str: String = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
        ///当颜色小于等于两位字符的时候，即0xFFFFFF  , 0xF5F5F5
        if str.count <= 2  {
            var c:UInt32 = 0x0
            //扫描字符串并且并且将十六进制转为十进制
            Scanner(string: str).scanHexInt32(&c)
            return UIColor(r: c, g: c, b: c)
        }
        if str.count >= 6 {
            ///索引
            let index = str.index(str.endIndex, offsetBy: -6)
            let subString = str[index...]
            if str.hasPrefix("0X") { str = String(subString) }
            if str.hasPrefix("#") { str = String(subString) }
            
            var range: NSRange = NSMakeRange(0, 2)
            let rString = (str as NSString).substring(with: range)
            range.location = 2
            let gString = (str as NSString).substring(with: range)
            range.location = 4
            let bString = (str as NSString).substring(with: range)
            
            var r: UInt32 = 0x0
            var g: UInt32 = 0x0
            var b: UInt32 = 0x0
            
            Scanner(string: rString).scanHexInt32(&r)
            Scanner(string: gString).scanHexInt32(&g)
            Scanner(string: bString).scanHexInt32(&b)
            
            return UIColor(r: r, g: g, b: b)
        }
        return .white
    }
}




extension UIView{
    //IBInspectable ib面板设置圆角和边框颜色
    
    @IBInspectable var cornerRadius: CGFloat{
        get{
            return layer.cornerRadius
        }
        set{
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }
    
    
    @IBInspectable var borderWidth: CGFloat{
        get{
            return layer.borderWidth
        }
        set{
            layer.borderWidth = newValue > 0 ? newValue : 0
        }
    }
    
    
    @IBInspectable var borderColor: UIColor{
        get{
            return UIColor(cgColor: layer.borderColor!)
        }
        set{
            layer.borderColor = newValue.cgColor
        }
        
    }
    
    
    func width() -> CGFloat{
        return self.frame.size.width
    }
    
    func height() -> CGFloat{
        return self.frame.size.height
    }
    
}

